﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess.DataAccess
{
    public class DepartmentsDataAccess : EducationalDivisionsDataAccess
    {
        public DepartmentsDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Department> SelectDepartmentsList()
        {
            List<EducationalDivision> divList = SelectEducationalDivisionsList();

            List<DEPARTMENT> depList = dataContext.DEPARTMENT.ToList();

            //Merge result
            var res = (from div in divList
                       join dep in depList on div.ID equals dep.ID
                       select new Department
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           SPECIALIZATION = dep.SPECIALIZATION,
                           MAX_POSTGRADUATES = (Int32)dep.MAX_POSTGRADUATES
                       }).ToList();

            return res;
        }

        public Department SelectDepartmentBy_Id(int Id)
        {
            try
            {
                EducationalDivision basePart = SelectEducationalDivisionBy_Id(Id);

                List<DEPARTMENT> depPart = dataContext.DEPARTMENT.ToList();

                //Merge result
                var res = (from dep in depPart
                           where dep.ID == basePart.ID
                           select new Department
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               SPECIALIZATION = dep.SPECIALIZATION,
                               MAX_POSTGRADUATES = (Int32)dep.MAX_POSTGRADUATES
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectDepartmentById");
            }
        }

        public void InsertDepartment(Department dep, ref int generated_Id)
        {
            try
            {
                InsertEducationalDivision(dep, ref generated_Id);

                int new_Id = generated_Id;
                DEPARTMENT depPart = new DEPARTMENT();
                depPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                depPart.ID = new_Id;
                depPart.SPECIALIZATION = dep.SPECIALIZATION;
                depPart.MAX_POSTGRADUATES = dep.MAX_POSTGRADUATES;

                //Submit changes
                dataContext.AddToDEPARTMENT(depPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertDepartment");
            }
        }

        public void UpdateDepartment(Department dep)
        {
            try
            {
                UpdateEducationalDivision(dep);

                DEPARTMENT depPart = dataContext.DEPARTMENT.Where(t => t.ID == dep.ID).FirstOrDefault();

                depPart.SPECIALIZATION = dep.SPECIALIZATION;
                depPart.MAX_POSTGRADUATES = dep.MAX_POSTGRADUATES;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateDepartment");
            }
        }

        public void DeleteDepartment(Department dep)
        {
            try
            {
                DEPARTMENT depPart = dataContext.DEPARTMENT.Where(t => t.ID == dep.ID).FirstOrDefault();

                dataContext.DeleteObject(depPart);
                dataContext.SaveChanges();

                DeleteEducationalDivision(dep);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteDepartment");
            }
        }
    }
}
